package com.qersoft.entity;

import lombok.Data;
import org.springframework.http.HttpStatus;

@Data
public class ResponseMessage<T> {

    private Integer code;
    private String message;
    private T data;

    public ResponseMessage(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResponseMessage<T> message(T data) {
        if (data != null) {
            if (data instanceof Boolean) {
                if ((Boolean) data) {
                    return new ResponseMessage(HttpStatus.OK.value(), "success!", data);
                } else {
                    return new ResponseMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), "error!", data);
                }
            }
            if (data instanceof Integer) {
                if ((Integer) data >= 0) {
                    return new ResponseMessage(HttpStatus.OK.value(), "success!", data);
                } else {
                    return new ResponseMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), "error!", data);
                }
            }
            if (data instanceof Object) {
                return new ResponseMessage(HttpStatus.OK.value(), "success!", data);
            }
            return new ResponseMessage(HttpStatus.OK.value(), "success!", data);
        }
            return new ResponseMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), "error", null);
    }

}
